#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 添加表头
    QStringList headers;
    headers << "Name" << "Age" << "Action";
    ui->tableWidget->setColumnCount(headers.size());
    ui->tableWidget->setHorizontalHeaderLabels(headers);
    //在 widget 窗口中添加一个 5 行 3 列的表格
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setRowCount(5);
    // 将水平表头列宽设置为自动拉伸，使所有列填充整个水平空间
   ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
   //将第0列（即索引为0的列）的列宽调整模式设置为根据内容自动调整。
   ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
   //设置水平表头背景色
   //TableWidget.horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
   //设置横向表头
       ui->tableWidget->horizontalHeader()->setStyleSheet(
           "QHeaderView::section{"
           "border-top:0px solid #E5E5E5;"        //设置上边界线
           "border-left:0px solid #E5E5E5;"       //设置左边界线
           "border-right:0.0px solid #E5E5E5;"    //设置右边界线
           "border-bottom: 0.0px solid #E5E5E5;"  //设置下边界线
           "background-color:transparent;"              //设置水平表头背景色
           "padding:4px;"                         //
           "}"
           );

    //设置QTableWidgt只有行线没有列线
    ui->tableWidget->setShowGrid(true);//设置false会隐藏表格线
   ui->tableWidget->setStyleSheet(
                "QTableWidget:Item{boder:0px, solid, rgb(0,255,0);"
                 "border-bottom:2px,solid,rgb(159,159,238);}"
                );
     //自定义表格的尺寸和字体大小
     ui->tableWidget->resize(701,470);
     ui->tableWidget->setFont(QFont("宋体",20));
    // 添加数据
    QList<QStringList> data = {
        {"Alice", "20", ""},
        {"Bob", "30", ""},
        {"Charlie", "40", ""}
    };
    int row = 0;
    for (const auto& rowData : data) {
        int col = 0;
        qDebug() << "rowData:" << rowData;
        for (const auto& cellData : rowData) {
            qDebug() << "row:" <<row <<"col:" << col;
            qDebug() << "cellData:" << cellData;
            QTableWidgetItem* item = new QTableWidgetItem(cellData);
            qDebug() << "item.text()=" <<item->text();
            ui->tableWidget->setItem(row, col, item);
            ++col;
        }
        QPushButton* button = new QPushButton("Button", this);
        connect(button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
        qDebug() << "row:" <<row <<"col:" << col;

        button->setProperty("row", row);
        button->setProperty("column", 2);
        ui->tableWidget->setCellWidget(row, 2, button);

        QPushButton* button2 = new QPushButton("click me", this);
        connect(button2, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
        qDebug() << "row:" <<row <<"col:" << col;

        button2->setProperty("row", row);
        button2->setProperty("column", 3);
        ui->tableWidget->setCellWidget(row, 3, button2);
        ++row;
     }


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_button_clicked()
{
    QObject* obj = sender(); // 返回发出信号的对象，用QObject类型接收
    QPushButton *button = qobject_cast<QPushButton *>(obj);   // 向下转型为按钮类型

    if (button) {
        // 获取按钮在table中的位置
        int row = button->property("row").toInt();
        int column = button->property("column").toInt();
        qDebug() << "Button clicked at (" << row << ", " << column << ")";
    }
}

